package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.manager.mapper.CategoryTblMapper;
import com.amigo.online.provider.manager.model.entity.CategoryTbl;
import com.amigo.online.provider.manager.service.CategoryTblService;
import com.amigo.online.provider.manager.util.param.dto.RedisKey;

@Service
public class CategoryTblServiceImpl implements CategoryTblService {
    @SuppressWarnings("rawtypes")
    @Autowired
    RedisTemplate redisTemplate;

    @Resource
    private CategoryTblMapper categoryTblMapper;

    @Override
    public List<CategoryTbl> findCategoryList(CategoryTbl categoryTbl) {

        return categoryTblMapper.findCategoryList(categoryTbl);
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean saveCategory(CategoryTbl categoryTbl) {
        categoryTbl.setDeleted(0);
        categoryTbl.setCreatedTime(System.currentTimeMillis() / 1000);
        redisTemplate.delete(RedisKey.MERCHANT_HOME_PAGE_CATEGORY_KEY);
        return categoryTblMapper.insert(categoryTbl) > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean updateCategory(CategoryTbl categoryTbl) {
        categoryTbl.setUpdatedTime(System.currentTimeMillis() / 1000);
        redisTemplate.delete(RedisKey.MERCHANT_HOME_PAGE_CATEGORY_KEY);
        return categoryTblMapper.updateCategory(categoryTbl) > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean deleteCategory(CategoryTbl categoryTbl) {
        categoryTbl.setDeleted(1);
        categoryTbl.setDeletedTime(System.currentTimeMillis() / 1000);
        redisTemplate.delete(RedisKey.MERCHANT_HOME_PAGE_CATEGORY_KEY);
        return categoryTblMapper.deleteCategory(categoryTbl) > 0;
    }

}
